package com.zyg.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyg.common.ResultCode;
import com.zyg.dao.SysRoleMapper;
import com.zyg.model.Result;
import com.zyg.model.ov.SysRoleTreeOV;
import com.zyg.model.query.SysRoleQueryParam;
import com.zyg.pojo.SysRole;
import com.zyg.service.SysRoleService;
import com.zyg.util.TreeBuildUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 牛二
 * @date: 2020/8/6 15:06
 * @email: 550354312@qq.com
 * @project: zyg_project_demo
 * @describe:
 */
@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private TreeBuildUtil treeBuildUtil;

    /**
     * 获取角色分页列表
     * @param queryParam
     * @return
     */
    @Override
    public Result getAllRolePageList(SysRoleQueryParam queryParam) {
        Page page = new Page(queryParam.getPage(),queryParam.getSize());
        baseMapper.selectPage(page,null);
        return new Result().initSuccess(page);
    }

    /**
     * 获取角色树形结构
     * @return
     */
    public Result getAllRoleTreeList() {
        List<SysRole> roleList = baseMapper.findAllRole();
        List<SysRoleTreeOV> sysRoleTreeOVList = new ArrayList<>();
        BeanUtils.copyProperties(roleList,sysRoleTreeOVList);
        for(SysRole sysRole : roleList){
            SysRoleTreeOV sysRoleTreeOV = new SysRoleTreeOV();
            BeanUtils.copyProperties(sysRole,sysRoleTreeOV);
            sysRoleTreeOVList.add(sysRoleTreeOV);
        }
        List<SysRole> tree = treeBuildUtil.treeBuilder(sysRoleTreeOVList);
        Result result = new Result().initCodeAndMsg(ResultCode.SUCCESS);
        result.setData(tree);
        return result;
    }

}
